package com.example.shoppingcart;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.example.shoppingcart.database.ShoppingCartDBHelper;
import com.example.shoppingcart.entity.GoodsInfo;
import com.example.shoppingcart.utils.ToastHelper;

public class GoodsDetial extends AppCompatActivity implements View.OnClickListener {

    private TextView tv_title;
    private ImageView iv_goods_image;
    private TextView tv_goods_price;
    private TextView tv_goods_desc;
    private Button btn_add_to_cart;
    private ShoppingCartDBHelper dbHelper;
    private TextView tv_count;
    private int goodsId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_goods_detial);

        tv_title = findViewById(R.id.tv_title);
        tv_count = findViewById(R.id.tv_count);
        iv_goods_image = findViewById(R.id.iv_goods_image);
        tv_goods_price = findViewById(R.id.tv_goods_price);
        tv_goods_desc = findViewById(R.id.tv_goods_desc);
        btn_add_to_cart = findViewById(R.id.btn_add_to_cart);

        findViewById(R.id.iv_back).setOnClickListener(this);
        findViewById(R.id.iv_cart).setOnClickListener(this);
        findViewById(R.id.btn_add_to_cart).setOnClickListener(this);

        TextView tv_count = findViewById(R.id.tv_count);
        tv_count.setText(String.valueOf(MyApplication.getInstance().goodsCountInCart));

        dbHelper = ShoppingCartDBHelper.getInstance(this);
    }

    @Override
    protected void onPostResume() {
        super.onPostResume();

        setDetail();
    }

    private void setDetail() {
        // 获取页面跳转参数
        goodsId = getIntent().getIntExtra("goods_id", 0);
        if (goodsId > 0) {
            GoodsInfo goods = dbHelper.queryGoodsInfoById(goodsId);
            tv_title.setText(goods.name);
            iv_goods_image.setImageURI(Uri.parse(goods.picPath));
            tv_goods_price.setText(String.valueOf((int) goods.price));
            tv_goods_desc.setText(goods.description);
        }

    }

    @Override
    public void onClick(View view) {
        int id = view.getId();

        if (id == R.id.iv_back) {
            // 返回
            finish();
        } else if (id == R.id.iv_cart) {
            // 跳转购物车
            Intent intent = new Intent(this, ShoppingCartActivity.class);
            startActivity(intent);
        } else if (id == R.id.btn_add_to_cart) {
            // 添加到购物车
            int count = ++MyApplication.getInstance().goodsCountInCart;
            tv_count.setText(String.valueOf(count));
            dbHelper.insertCartInfo(goodsId);
            ToastHelper.show(this, "成功添加到购物车");
        }
    }
}